package co.com.inventarios.persistencia;

import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;

import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import co.com.inventarios.modelo.Bodega;
import co.com.inventarios.modelo.Cataventa;
import co.com.inventarios.modelo.Dlluser;
import co.com.inventarios.modelo.Producto;
import co.com.inventarios.modelo.RequiDoc;
import co.com.inventarios.modelo.RequiDocId;

public class QueryBodegaDAO {

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	public List<Bodega> Buscar_Bodegas_Aut(String Buscar) {

		String[] Bod = null;
		String filtro = "";
		Bod = Buscar.split(",");

		for (int i = 0; i < Bod.length; i++) {
			// System.out.println(Bod [i]);

			if (i == 0) {
				filtro = filtro + " AND (model.bodcodigo='" + Bod[i] + "' ";
			} else {
				filtro = filtro + " OR model.bodcodigo='" + Bod[i] + "' ";
			}
		}
		
		filtro = filtro + ")";
		
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodactivo = 1 "
					+ filtro;
			// +" order by model.bodcodigo DESC";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Bodega> findByPropertyLike(String propertyName, Object value,
			String autorizadas) {

		String[] Bod = null;
		String filtro = "";
		Bod = autorizadas.split(",");

		for (int i = 0; i < Bod.length; i++) {
			// System.out.println(Bod [i]);

			if (i == 0) {
				filtro = filtro + " AND (model.bodcodigo='" + Bod[i] + "' ";
			} else {
				filtro = filtro + " OR model.bodcodigo='" + Bod[i] + "' ";
			}
		}
		
		filtro = filtro + ")";
		
		EntityManagerHelper.log("finding Bodega instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model."
					+ propertyName + " LIKE '%" + value + "%'  " + filtro;
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public Bodega findByBodegaId(String value, String autorizadas) {

		String[] Bod = null;
		String filtro = "";
		Bodega resulbod = null;
		Bod = autorizadas.split(",");

		for (int i = 0; i < Bod.length; i++) {
			// System.out.println(Bod [i]);

			if (i == 0) {
				filtro = filtro + " AND( model.bodcodigo='" + Bod[i] + "' ";
			} else {
				filtro = filtro + " OR model.bodcodigo='" + Bod[i] + "' ";
			}

		}
		filtro = filtro + ")";

		EntityManagerHelper.log("finding Bodega instance with property: "
				+ "bodcodigo" + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where (model."
					+ "bodcodigo" + " = '" + value + "')  " + filtro;
			Query query = getEntityManager().createQuery(queryString);

			if (query.getResultList().size() > 0) {
				resulbod = (Bodega) query.getResultList().get(0);
			}

			return resulbod;

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}


	public List<Bodega> Buscar_Bodegas_Aut_all() {

		
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodactivo = 1 ";
			// +" order by model.bodcodigo DESC";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Bodega> findByPropertyLike_all(String propertyName, Object value) {

		
		EntityManagerHelper.log("finding Bodega instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model."
					+ propertyName + " LIKE '%" + value + "%'  ";
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public Bodega findByBodegaId_all(String value) {

		Bodega resulbod = null;

		EntityManagerHelper.log("finding Bodega instance with property: "
				+ "bodcodigo" + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where (model."
					+ "bodcodigo" + " = '" + value + "')  ";
			Query query = getEntityManager().createQuery(queryString);

			if (query.getResultList().size() > 0) {
				resulbod = (Bodega) query.getResultList().get(0);
			}
			return resulbod;

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	
	
	
	
	public List<Bodega> findByPropertyAll(String propertyName, Object value) {

		EntityManagerHelper.log("finding Bodega instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model."
					+ propertyName + " LIKE '%" + value + "%'  " ;
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	
	public Bodega findByPropertyActivo(String propertyName, Object value) {
		// TODO Auto-generated method stub
		EntityManagerHelper.log("finding Bodega instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodactivo = 1 AND model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			
			Bodega result = new Bodega(); 
			if (query.getResultList().size() > 0){
				result = (Bodega)query.getSingleResult();
			}else{
				result = null;
			}
				
			return result;
			
			
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}	
	
	
	
	public List<Bodega> findByPropertyActivoLista(String propertyName) {
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodactivo = 1 "+propertyName+" ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}	
	
	
	public Bodega findByBodega_hija(String bodegap,String bodegah) {
		
		Bodega resulbod = null;
		
		EntityManagerHelper.log("finding Bodega instance with property: "
				+ "bodcodigo" + ", value: " + bodegah, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodcodigo = '"
					+bodegah+ "' and model.bodpadre = '"+bodegap+"' and model.bodactivo = 1 ";
			
			Query query = getEntityManager().createQuery(queryString);

			if (query.getResultList().size() > 0) {
				resulbod = (Bodega) query.getResultList().get(0);
			}

			return resulbod;

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	public List<Bodega> findByBodega_hijaList(String bodegap,String propertyName, String value) {

		EntityManagerHelper.log("finding Bodega instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where model.bodpadre = '"+bodegap+"' and model.bodactivo = 1 and  model."
					+ propertyName + " LIKE '%" + value + "%'  " ;
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}	
	
	public List<Bodega> findByBodega_hijaListAll(String bodegap) {

		EntityManagerHelper.log("finding Bodega instance with property: "
				+ bodegap + ", value: " + bodegap, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Bodega model where  model.bodpadre = '"+bodegap+"' and model.bodactivo = 1  " ;
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
}
